home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 6984 / 6984.xpi / chrome / lazarus.jar / content / crypto.js < prev    next >
Encoding:
JavaScript  |  2009-11-24  |  5.0 KB  |  146 lines

  1.  
  2. /**
  3. * Lazarus Cryptography
  4. * We will use hybrid encryption when saving data to the database.
  5. * this should allow us to encrypt forms with the public key and only require the user to 
  6. * enter their password when they want to retrieve a form.
  7. * Both the public and private (RSA) keys should be kept in the database.
  8. * The public key is kept unencrypted, but the private key will be symetrically encrypted (AES)
  9. * using the users password.
  10. */
  11.  
  12.  
  13. /**
  14. * retrieve the public key from the lazarus database
  15. */
  16. Lazarus.loadPublicKey = function(){
  17.     if (!Lazarus.Crypto.publicKey){
  18.         var b64Key = Lazarus.db.getStr("SELECT value FROM settings WHERE name = 'public-key'");
  19.         if (b64Key){
  20.             Lazarus.Crypto.publicKey = Lazarus.keyFromString(b64Key);    
  21.         }
  22.     }
  23.     return Lazarus.Crypto.publicKey ? true : false;
  24. }
  25.  
  26. /**
  27. * attempt to load the private key
  28. */
  29. Lazarus.loadPrivateKey = function(password){
  30.  
  31.     if (typeof(password) === 'undefined'){
  32.         password = Lazarus.loadPassword();
  33.     }
  34.     
  35.     if (!Lazarus.Crypto.privateKey){
  36.         var encb64Key = Lazarus.db.getStr("SELECT value FROM settings WHERE name = 'private-key'");
  37.         //we need to unencrypt the private key
  38.         Lazarus.Crypto.isPasswordEntered = (password) ? true : false;
  39.         var b64Key = Lazarus.Crypto.AESDecrypt(encb64Key, password);
  40.         if (b64Key){
  41.             Lazarus.Crypto.privateKey = Lazarus.keyFromString(b64Key);    
  42.         }
  43.     }
  44.     return Lazarus.Crypto.privateKey ? true : false;
  45. }
  46.  
  47. Lazarus.unloadPrivateKey = function(){ 
  48.     Lazarus.Crypto.isPasswordEntered = null;
  49.     Lazarus.Crypto.privateKey = null;
  50. }
  51.  
  52. Lazarus.initEncryptionKeys = function(){
  53.     if (Lazarus.loadPublicKey()){
  54.         //attempt to load the private key as well (this will fail if the user is not logged in, but thats ok)
  55.         Lazarus.loadPrivateKey();
  56.         return true;
  57.     }
  58.     else {
  59.         //using setTimeout so that firefox can continue to load if we need to generate keys (eg first run)
  60.         setTimeout(function(){
  61.             //FRAK: key generation requires that a user is logged in to the Software Security Device (if they have a master password set!)
  62.             //Bugger, this is exactly what we were trying to avoid with the hybrid encryption stuff in the first place!
  63.             if (Lazarus.isMasterPasswordRequired()){
  64.                 Lazarus.openGenerateKeysDialog();                
  65.                 if (Lazarus.reloadKeys()){
  66.                     Lazarus.enable();
  67.                 }
  68.             }
  69.             else {
  70.                 Lazarus.generateEncryptionKeys();
  71.                 Lazarus.enable();
  72.             }
  73.         }, 1);
  74.         return false;
  75.     }
  76. }
  77.  
  78. Lazarus.reloadKeys = function(){
  79.     Lazarus.Crypto.publicKey = null;
  80.     Lazarus.unloadPrivateKey();
  81.     Lazarus.loadPrivateKey();
  82.     return Lazarus.loadPublicKey();
  83. }
  84.  
  85. /**
  86. * generate the public/private key pair and save them to the database.
  87. */
  88. Lazarus.generateEncryptionKeys = function(){
  89.  
  90.     Lazarus.Crypto.generatingKeys = true;
  91.     Lazarus.refreshIcon();    
  92.     
  93.     //Key generation can take a long time (up to 30 seconds on an old machine)
  94.     //so we'll do it in a background thread
  95.     var keyPair = null;
  96.     var generateKeysThreadComplete = false;
  97.  
  98.     var generateKeysThread = {
  99.         run: function() {
  100.             //perform work here that doesn't touch the DOM or anything else that isn't thread safe
  101.             keyPair = Lazarus.Crypto.generateRSAKeyPair();
  102.             generateKeysThreadComplete = true;
  103.         }
  104.     }
  105.     
  106.     var thread = Components.classes["@mozilla.org/thread-manager;1"].getService(Components.interfaces.nsIThreadManager).newThread(0);
  107.     thread.dispatch(generateKeysThread, thread.DISPATCH_NORMAL);
  108.     
  109.     var currThread = Components.classes["@mozilla.org/thread-manager;1"].getService(Components.interfaces.nsIThreadManager).currentThread;
  110.     while (!generateKeysThreadComplete){
  111.         currThread.processNextEvent(true);
  112.     }
  113.     
  114.     if (keyPair){
  115.         //now save to the database
  116.         //keys have been generated, time to save them to the database.
  117.         Lazarus.db.exe("DELETE FROM settings WHERE name = 'public-key'");
  118.         Lazarus.db.exe("INSERT INTO settings (name, value) VALUES ('public-key', ?1)", Lazarus.keyToString(keyPair.public));
  119.         
  120.         Lazarus.db.exe("DELETE FROM settings WHERE name = 'private-key'");
  121.         Lazarus.db.exe("INSERT INTO settings (name, value) VALUES ('private-key', ?1)", Crypto.AESEncrypt(Lazarus.keyToString(keyPair.private), ""));
  122.         
  123.         //and truncate the forms table as all previous forms will no longer be able to be decrypted
  124.         Lazarus.emptyDB();
  125.     }
  126.     else {
  127.         //FIXME: this is a FATAL error, user should be informed
  128.         Lazarus.error("Failed to generate key pair!");
  129.         Lazarus.disable();
  130.     }
  131.     
  132.     Lazarus.Crypto.generatingKeys = false;
  133.     Lazarus.reloadKeys();
  134.     Lazarus.refreshIcon();  
  135. }
  136.  
  137.  
  138. Lazarus.keyToString = function(key){
  139.     return btoa(Lazarus.JSON.encode(key));
  140. }
  141.  
  142. Lazarus.keyFromString = function(base64Str){
  143.     return Lazarus.JSON.decode(atob(base64Str));
  144. }
  145.